let str = `To be, or not to be, that is the question:
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune,
Or to take Arms against a Sea of troubles,
And by opposing end them: to die, to sleep
No more; and by a sleep, to say we end
The heart-ache, and the thousand natural shocks
That Flesh is heir to? 'Tis a consummation
Devoutly to be wished. To die, to sleep,
To sleep, perchance to Dream; aye, there's the rub,
For in that sleep of death, what dreams may come,
When we have shuffled off this mortal coil,
Must give us pause. There's the respect
That makes Calamity of so long life:
For who would bear the Whips and Scorns of time,
The Oppressor's wrong, the proud man's Contumely,
The pangs of dispised Love, the Law's delay,
The insolence of Office, and the spurns
That patient merit of th'unworthy takes,
When he himself might his Quietus make
With a bare Bodkin? Who would Fardels bear,
To grunt and sweat under a weary life,
But that the dread of something after death,
The undiscovered country, from whose bourn
No traveller returns, puzzles the will,
And makes us rather bear those ills we have,
Than fly to others that we know not of?
Thus conscience does make cowards of us all,
And thus the native hue of Resolution
Is sicklied o'er, with the pale cast of Thought,
And enterprises of great pitch and moment,
With this regard their Currents turn awry,
And lose the name of Action. Soft you now,
The fair Ophelia? Nymph, in thy Orisons
Be all my sins remember'd.`
//题目理解：单词按字母排序组成一段字符串  （单词首字母必须大写、每个首字母相同的单词取一个长度最长拼接、若长度相同用从第二个字母再排序取最大）

//1.把除了A-z'-及空白符的字符去除 2.把换行符换成空白符
let newStr = str.replace(/[^A-z'-\s]/g, '').replace(/\n/g, ' ')
//1.用空白符分割成数组 2.去除首字母不是A-z的值  3.排序
let newArr = newStr.split(' ').filter(item => /[A-z]/.test(item.slice(0, 1))).map(item => item.slice(0, 1).toUpperCase() + item.slice(1).toLowerCase()).sort()
console.log(newArr);
let arr = []
let index = 0
newArr.reduce((pre, cur) => {
  //排序后判断前一个和后一个首字母是否相同
  if (pre.charAt(0) == cur.charAt(0)) {
    //长度长的放进arr再返回
    if (pre.length <= cur.length) {
      arr[index] = cur
    } else {
      arr[index] = pre
    }
    return arr[index]
  } else {
    //首字母不同则放进下一个index并返回初始值
    index = index + 1
    arr[index] = cur
    return cur
  }
})
console.log(arr);
/* 
['Against',      'Bodkin',
  'Consummation', 'Dispised',
  'Enterprises',  'Fortune',
  'Grunt',        'Heart-ache',
  'Insolence',    'Know',
  "Law's",        'Mortal',
  'Natural',      "Oppressor's",
  'Perchance',    'Question',
  'Resolution',   'Something',
  "Th'unworthy",  'Undiscovered',
  'Whether',      'You'] */

let result = arr.join('')
console.log(result);
//AgainstBodkinConsummationDispisedEnterprisesFortuneGruntHeart-acheInsolenceKnowLaw'sMortalNaturalOppressor'sPerchanceQuestionResolutionSomethingTh'unworthyUndiscoveredWhetherYou




console.log('1');

setTimeout(function () {
  console.log('2');
  process.nextTick(function () {
    console.log('3');
  })
  new Promise(function (resolve) {
    console.log('4');

    resolve();
  }).then(function () {
    console.log('5')
  })
})
process.nextTick(function () {
  console.log('6');
})
new Promise(function (resolve) {
  console.log('7');
  resolve();
}).then(function () {
  console.log('8')
})

setTimeout(function () {
  console.log('9');
  process.nextTick(function () {
    console.log('10');
  })
  new Promise(function (resolve) {
    console.log('11');
    resolve();
  }).then(function () {
    console.log('12')
  })
})